/*
 * File: vfsdriver.c
 *
 * Description: This is a driver program for testing your VFS system using an interaction script as input
 * You need to make additional calls to your respective functions as noted in the comments below
 * Make sure the output you display is exactly as per the given specifications for you. Do NOT print
 * any extra output (like debug messages) in the final version of your driver program. You can use this driver program
 * in a in incremental manner as you keep implementing one operator after another. For operators not yet implemented,
 * you can leave the output as given ("TO_BE_DONE"). So you can use this program as your "main" program for testing purposes.
 *
 * DO NOT write the full code for operators in the driver program! You must only CALL your functions from here.
 *
 * Usage: vfsdriver <scriptfilename>
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/header.h"
#include "../include/errors.h"
#include "../include/dir_op.h"
#include "../include/file_operations.h"
#include "../include/value_added.h"

#define BUFSIZE 200
#define CMDSIZE 30
#define PARSIZE 100

void createvfs ( char *P1, int P2 );
void mountvfs ( char *P1 );
void unmountvfs ( char *P1 );
void makedir ( char *P1, char *P2 );
void deletedir ( char *P1 );
void movedir ( char *P1, char *P2 );
void listdir ( char *P1, int P2, char *P3 );
void addfile ( char *P1, char *P2, char *P3 );
void listfile ( char *P1, char *P2 );
void updatefile ( char *P1, char *P2 );
void removefile ( char *P1 );
void movefile ( char *P1, char *P2 );
void copyfile ( char *P1, char *P2 );
void exportfile ( char *P1, char *P2 );
void searchfile ( char *P1, char *P2 );
void print_tree(char *P1,char *P2);
void rename_file(char *P1,char *P2);
void processcommand( char *command, char *P1, char *P2, char *P3 );

int main( int argc, char *argv[] )
{
    FILE *scriptfp;
    char linebuffer[BUFSIZE];
    char command[CMDSIZE], par1[PARSIZE], par2[PARSIZE], par3[PARSIZE];
    char *token;

    if( argc != 2 )
    {
        fprintf(stderr,"Usage: vfsdriver <scriptfile>\n");
        return(1);
    }

    if( (scriptfp=fopen(argv[1],"r")) == NULL )
    {
        fprintf(stderr,"Unable to open script file: %s\n", argv[1]);
        return(2);
    }

    while( fgets(linebuffer, sizeof(linebuffer), scriptfp) != NULL )
    {
        /* This output is for debugging... do not uncomment in final version */
        /*
        printf("==================================================\n");
        printf("Processing: %s", linebuffer);
        printf("===============================================printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_02);===\n");
        */

        /* Remove the extra newline character in the end of line */
        linebuffer[ strlen(linebuffer)-1 ] = '\0';

        /* Get the command and the parameters using tokenizer */
        strcpy( command, (token = strtok(linebuffer, " ")) == NULL ? "" : token );

        strcpy( par1, (token = strtok(NULL, " ")) == NULL ? "" : token );
        strcpy( par2, (token = strtok(NULL, " ")) == NULL ? "" : token );
        strcpy( par3, (token = strtok(NULL, " ")) == NULL ? "" : token );
        /*printf("Command:%s:p1:%s:p2:%s:p3:%s\n",command, par1, par2, par3);*/ 

        processcommand( command, par1, par2, par3 );
    }
}

void processcommand( char *command, char *P1, char *P2, char *P3 )
{
    if( strcmp(command, "createvfs") == 0 )
    {
        int size = atoi(P2);
        createvfs (P1,size);
    }
    else if( strcmp(command, "mountvfs") == 0 )
        mountvfs (P1);
    else if( strcmp(command, "unmountvfs") == 0 )
        unmountvfs (P1);
    else if( strcmp(command, "makedir") == 0 )
        makedir (P1,P2);
    else if( strcmp(command, "deletedir") == 0 )
        deletedir (P1);
    else if( strcmp(command, "movedir") == 0 )
        movedir (P1,P2);
    else if( strcmp(command, "listdir") == 0 )
    {
        int flag = atoi(P2);
        listdir (P1,flag,P3);
    }
    else if( strcmp(command, "addfile") == 0 )
        addfile (P1,P2,P3);
    else if( strcmp(command, "listfile") == 0 )
        listfile (P1,P2);
    else if( strcmp(command, "updatefile") == 0 )
        updatefile (P1,P2);
    else if( strcmp(command, "removefile") == 0 )
        removefile (P1);
    else if( strcmp(command, "movefile") == 0 )
        movefile (P1,P2);
    else if( strcmp(command, "copyfile") == 0 )
        copyfile (P1,P2);
    else if( strcmp(command, "exportfile") == 0 )
        exportfile (P1,P2);
    else if( strcmp(command, "searchfile") == 0 )
        searchfile (P1,P2);
    else if( strcmp(command, "treedir") == 0 )
        tree_dir(P1,P2);
    else if( strcmp(command, "renamefile") == 0 )
        rename_file(P1,P2);
    else
        printf("Ignoring invalid command %s\n", command);
}

void createvfs ( char *P1, int P2 )
{
    int ret=-1;
    ret=create_vfs(P1,P2);
#if DEBUG
    printf("ret:%d\n",ret);
#endif
    if(ret==0)
        printf("createvfs_FAILURE %s\n",ERR_VFS_CREATE_00);
    else if(ret==2)
        printf("createvfs_FAILURE %s\n",ERR_VFS_CREATE_02);
    else if(ret==1)
        printf("createvfs_FAILURE %s\n",ERR_VFS_CREATE_01 );
    else if(ret==4)
        printf("createvfs_FAILURE %s\n",ERR_VFS_CREATE_04);
    else if(ret==5)
        printf("createvfs_FAILURE %s\n",ERR_VFS_CREATE_05);
    else if(ret==7)
        printf("createvfs_FAILURE %s\n",ERR_VFS_CREATE_07);
    else if(ret==10)
        printf("createvfs_SUCCESS\n");
    else
        printf("createvfs_FAILURE\n");

}

void mountvfs ( char *P1 )
{
    int ret=-1;

    ret=mount_vfs(P1);

#if DEBUG
    printf("ret:%d\n",ret);
#endif
    if(ret==0)
        printf("mountvfs_FAILURE %s\n",ERR_VFS_MOUNT_00) ;
    else if(ret==1)
        printf("mountvfs_FAILURE %s\n",ERR_VFS_MOUNT_01) ;
    else if(ret==2)
        printf("mountvfs_FAILURE %s\n",ERR_VFS_MOUNT_02);
    else if(ret==4)
        printf("mountvfs_FAILURE %s\n",ERR_VFS_MOUNT_04);
    else if(ret==5)
        printf("mountvfs_FAILURE %s\n",ERR_VFS_MOUNT_05);
    else if(ret==10)
        printf("mountvfs_SUCCESS\n");
    else
        printf("mountvfs_FAILURE\n");


}

void unmountvfs ( char *P1 )
{
    int ret=unmount_vfs(P1);
#if DEBUG
    printf("ret:%d\n",ret);
#endif
    if(ret==0)
        printf("unmountvfs_FAILURE %s\n",ERR_VFS_UNMOUNT_00);
    else if(ret==1)
        printf("unmountvfs_FAILURE %s\n",ERR_VFS_UNMOUNT_01);
    else if(ret==2)
        printf("unmountvfs_FAILURE %s\n",ERR_VFS_UNMOUNT_02);
    else if(ret==3)
        printf("unmountvfs_FAILURE %s\n",ERR_VFS_UNMOUNT_03);
    else if(ret==4)
        printf("unmountvfs_FAILURE %s\n",ERR_VFS_UNMOUNT_04);
    else if(ret==10)
        printf("unmountvfs_SUCCESS\n");
    else
        printf("unmountvfs_FAILURE\n");
}

void makedir ( char *P1, char *P2 )
{
    int ret=-1;
    ret=create_dir(P1,P2);
    //printf("\nret %d",ret);
    if(ret==0)
        printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_00);
    else if(ret==1)
        printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_01);
    else if(ret==2)
        printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_02);
    else if(ret==3)
        printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_03);
    else if(ret==4)
        printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_05);
    else if(ret==10)
        printf("makedir_SUCCESS\n");
    else
        printf("MAKEDIR_FAILURE_REASON_NOT_KNOWN\n");
}

void deletedir ( char *P1)
{
    int ret=-1;

    ret=delete_dir(P1);

    if(ret==0)
        printf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_00);
    else if(ret==1)
        printf("deletedir_FAILURE %s\n",  ERR_VFS_DELETEDIR_01);
    else if(ret==2)
        printf("deletedir_FAILURE %s\n",  ERR_VFS_DELETEDIR_02);
    else if(ret==4)
        printf("deletedir_FAILURE %s\n", ERR_VFS_DELETEDIR_04);
    else if(ret==10)
        printf("deletedir_SUCCESS\n");
    else
        printf("DELETEDIR_FAILURE_REASON_NOT_KNOWN\n");
}

void movedir ( char *P1, char *P2 )

{
    int ret=-1;
    ret=move_dir(P1,P2);
    if(ret==0)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_00);
    else if(ret==1)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_01);
    else if(ret==2)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_02);
    else if(ret==4)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_04);
    else if(ret==5)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_05);
    else if(ret==6)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_06);
    else if(ret==7)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_07);
    else if(ret==8)
        printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_08);
    else if(ret==10)
        printf("movedir_SUCCESS\n");
    else
        printf("MOVEDIR_FAILURE_REASON_NOT_KNOWN\n");


}

void listdir ( char *P1, int P2, char *P3)
{
    int ret=-1;
    FILE *fp;
    ret=list_direc(P1,P2,P3);
    if(ret==0)
        printf("listdir_FAILURE %s\n",ERR_VFS_DELETEDIR_00);
    else if(ret==1)
        printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_01);
    else if(ret==2)
        printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_02);
    else if(ret==3)
        printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_03);
    else if(ret==4)
        printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_04);
    else if(ret==107)
        printf("listdir_SUCCESS\n");
    else
        printf("LISTDIR_FAILURE_REASON_NOT_KNOWN\n");
}

void addfile ( char *P1, char *P2, char *P3 )
{
    int ret=0;
    ret=add_file(P1,P2,P3);
    if(ret==0)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_00);
    else if (ret==1)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_01);
    else if(ret==2)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_02);
    else if(ret==3)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_03);
    else if(ret==4)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_04);
    else if(ret==5)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_05);
    else if(ret==6)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_06);
    else if(ret==7)
        printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_07);
    else if(ret==10)
        printf("addfile_SUCCESS\n");
    else
        printf("addfile_FAILURE\n");

}

void listfile ( char *P1, char *P2 )
{
    int ret=list_file(P1,P2);
    if(ret==1)
        printf("listfile_FAILURE %s\n",ERR_VFS_LISTFILE_01);
    else if(ret==0)
        printf("lisfile_FAILURE %s\n", ERR_VFS_LISTFILE_00);
    else if(ret==2)
        printf("listfile_FAILURE %s\n",ERR_VFS_LISTFILE_02);
    else if(ret==5)
        printf("listfile_FAILURE %s\n",ERR_VFS_LISTFILE_05);
    else if(ret==6)
        printf("listfile_FAILURE %s\n",ERR_VFS_LISTFILE_06);
    else if(ret==10)
        printf("lsitfile_SUCCESS\n");
    else
        printf("listfile_FAILURE\n");
}

void updatefile ( char *P1, char *P2 )
{
    int ret=Update_file(P1,P2);
    if(ret==0)
        printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_00);
    else if(ret==1)
        printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_01);
    else if(ret==2)
        printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_02);
    else if(ret==4)
        printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_04);
    else if(ret==6)
        printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_06);
    else if(ret==10)
        printf("updatefile_SUCCESS \n");
    else
        printf("updatefile_FAILURE\n");

}

void removefile ( char *P1 )
{
    int ret=remove_file(P1);
    if(ret==0)
        printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_00);
    else if(ret==1)
        printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_01);
    else if(ret==2)
        printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_02);
    else if(ret==10)
        printf("removefile_SUCCESS\n");
    else
        printf("removefile_FAILURE\n");
}

void movefile ( char *P1, char *P2 )
{
    int ret=-1;
    ret=move_file(P1,P2);
    if(ret==0)
        printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_00);
    else if(ret==1)
        printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_01);
    else if(ret==2)
        printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_02);
    else if(ret==6)
        printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_06);
    else if(ret==10)
        printf("movefile_SUCCESS\n");
    else
        printf("MOVEFILE_FAILURE_REASON_NOT_KNOWN\n");
}

void copyfile ( char *P1, char *P2 )
{
    int ret=copy_file(P1,P2);

    if(ret==1)
        printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_01);
    else if(ret==0)
        printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_00);
    else if(ret==4)
        printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_04);
    else if(ret==5)
        printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_05);
    else if(ret==6)
        printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_06);
    else if(ret==7)
         printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_07);
    else if(ret==10)
        printf("copyfile_SUCCESS \n");
    else
        printf("copyfile_FAILURE\n");

}

void exportfile ( char *P1, char *P2 )
{
    int ret=export_file(P1,P2);
    
    if(ret==1)
        printf("exportfile_FAILURE %s\n",ERR_VFS_EXPORTFILE_01);
    else if(ret==0)
        printf("exportfile_FAILURE %s\n",ERR_VFS_EXPORTFILE_00);
    else if(ret==2)
        printf("exportfile_FAILURE %s\n",ERR_VFS_EXPORTFILE_02);
    else if(ret==6)
        printf("exportfile_FAILURE %s\n",ERR_VFS_EXPORTFILE_06);
    else if(ret==7)
        printf("exportfile_FAILURE %s\n",ERR_VFS_EXPORTFILE_07);
    else if(ret==10)
        printf("exportfile_SUCCESS\n");
    else
        printf("exportfile_FAILURE\n");
}

void searchfile (char *P1, char *P2 )
{
    int ret=-1;
    ret=search_File(P1,P2);
    if(ret==0)
        printf("searchfile_FAILURE %s\n",ERR_VFS_SEARCHFILE_00);
    else if(ret==1)
        printf("searchfile_FAILURE %s\n",ERR_VFS_SEARCHFILE_01);
    else if(ret==10)
        printf("searchfile_SUCCESS\n");
    else
        printf("SEARCHFILE_FAILURE_REASON_NOT_KNOWN\n");

}

void print_tree(char *P1,char *P2){
  int ret=-1;
  ret=tree_dir(P1,P2); 
}


void rename_file(char *P1,char *P2){
  int ret=-1;
  ret=rename_file_(P1,P2);    
  //printf("rename %d",ret);
  
     

}
